<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/* 
<span class='line'>  2</span>  * Timemap.js Copyright 2010 Nick Rabinowitz.
<span class='line'>  3</span>  * Licensed under the MIT License (see LICENSE.txt)
<span class='line'>  4</span>  */</span><span class="WHIT">
<span class='line'>  5</span> </span><span class="WHIT"> </span><span class="WHIT">
<span class='line'>  6</span> </span><span class="COMM">/**
<span class='line'>  7</span>  * @fileOverview
<span class='line'>  8</span>  * Metaweb Loader
<span class='line'>  9</span>  *
<span class='line'> 10</span>  * @author Nick Rabinowitz (www.nickrabinowitz.com)
<span class='line'> 11</span>  */</span><span class="WHIT">
<span class='line'> 12</span> </span><span class="WHIT"> 
<span class='line'> 13</span> </span><span class="COMM">// for JSLint</span><span class="WHIT">
<span class='line'> 14</span> </span><span class="COMM">/*global TimeMap */</span><span class="WHIT">
<span class='line'> 15</span> 
<span class='line'> 16</span> </span><span class="COMM">/**
<span class='line'> 17</span>  * @class
<span class='line'> 18</span>  * Metaweb loader: Load data from freebase.com.
<span class='line'> 19</span>  *
<span class='line'> 20</span>  * &lt;p>This is a loader for data from the Metaweb service at freebase.com. See
<span class='line'> 21</span>  * the API documentation at &lt;a href="http://www.freebase.com/docs/mql/ch01.html">http://www.freebase.com/docs/mql/ch01.html&lt;/a> for
<span class='line'> 22</span>  * a description of how to write MQL queries. This code is based on code from
<span class='line'> 23</span>  * the API site.&lt;/p>
<span class='line'> 24</span>  *
<span class='line'> 25</span>  * @augments TimeMap.loaders.jsonp
<span class='line'> 26</span>  * @requires lib/json2.pack.js
<span class='line'> 27</span>  * @requires loaders/jsonp.js
<span class='line'> 28</span>  *
<span class='line'> 29</span>  * @example
<span class='line'> 30</span> TimeMap.init({
<span class='line'> 31</span>     datasets: [
<span class='line'> 32</span>         {
<span class='line'> 33</span>             title: "Freebase Dataset",
<span class='line'> 34</span>             type: "metaweb",
<span class='line'> 35</span>             options: {
<span class='line'> 36</span>                 query: [
<span class='line'> 37</span>                     {
<span class='line'> 38</span>                       // query here - see Metaweb API
<span class='line'> 39</span>                     }
<span class='line'> 40</span>                 ],
<span class='line'> 41</span>                 transformFunction: function(data) {
<span class='line'> 42</span>                     // map returned data to the expected format - see
<span class='line'> 43</span>                     // http://code.google.com/p/timemap/wiki/JsonFormat
<span class='line'> 44</span>                     return data;
<span class='line'> 45</span>                 }
<span class='line'> 46</span>             }
<span class='line'> 47</span>         }
<span class='line'> 48</span>     ],
<span class='line'> 49</span>     // etc...
<span class='line'> 50</span> });
<span class='line'> 51</span>  * @see &lt;a href="../../examples/artists.html">Metaweb Example&lt;/a>
<span class='line'> 52</span>  *
<span class='line'> 53</span>  * @param {Object} options          All options for the loader
<span class='line'> 54</span>  * @param {Object} options.query                MQL query to load
<span class='line'> 55</span>  * @param {Function} options.transformFunction  Function to call on individual items before loading
<span class='line'> 56</span>  * @param {String} [options.host=http://www.freebase.com]   Host url of web service
<span class='line'> 57</span>  * @param {String} [options.service=/api/service/mqlread]   Path to web service on host
<span class='line'> 58</span>  * @param {mixed} [options[...]]    Other options (see {@link TimeMap.loaders.jsonp})
<span class='line'> 59</span>  */</span><span class="WHIT">
<span class='line'> 60</span> </span><span class="NAME">TimeMap.loaders.metaweb</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">options</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">loader</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">TimeMap.loaders.jsonp</span><span class="PUNC">(</span><span class="NAME">options</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 62</span> </span><span class="WHIT">        </span><span class="NAME">q</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">options.query</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 63</span> </span><span class="WHIT">        </span><span class="COMM">// format the query URL for Metaweb</span><span class="WHIT">
<span class='line'> 64</span> </span><span class="WHIT">        </span><span class="NAME">querytext</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">encodeURIComponent</span><span class="PUNC">(</span><span class="NAME">JSON.stringify</span><span class="PUNC">(</span><span class="PUNC">{</span><span class="NAME">qname</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="NAME">query</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">q</span><span class="PUNC">}</span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 65</span> </span><span class="WHIT">    
<span class='line'> 66</span>     </span><span class="COMM">/**
<span class='line'> 67</span>      * Host url - default to freebase.com
<span class='line'> 68</span>      * @name TimeMap.loaders.metaweb#HOST
<span class='line'> 69</span>      * @type {String}
<span class='line'> 70</span>      */</span><span class="WHIT">
<span class='line'> 71</span> </span><span class="WHIT">    </span><span class="NAME">loader.HOST</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">options.host</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="STRN">"http://www.freebase.com"</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 72</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'> 73</span>      * Service path - default to freebase.com default
<span class='line'> 74</span>      * @name TimeMap.loaders.metaweb#QUERY_SERVICE
<span class='line'> 75</span>      * @type {String}
<span class='line'> 76</span>      */</span><span class="WHIT">
<span class='line'> 77</span> </span><span class="WHIT">    </span><span class="NAME">loader.QUERY_SERVICE</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">options.service</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="STRN">"/api/service/mqlread"</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 78</span> 
<span class='line'> 79</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'> 80</span>      * URL built using encoded query text and the callback name
<span class='line'> 81</span>      * @name TimeMap.loaders.metaweb#url
<span class='line'> 82</span>      * @type {String}
<span class='line'> 83</span>      */</span><span class="WHIT">
<span class='line'> 84</span> </span><span class="WHIT">    </span><span class="NAME">loader.url</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">loader.HOST</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">loader.QUERY_SERVICE</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">"?queries="</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">querytext</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">"&callback="</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 85</span> </span><span class="WHIT">    
<span class='line'> 86</span>     </span><span class="COMM">/**
<span class='line'> 87</span>      * Preload function for Metaweb
<span class='line'> 88</span>      * @name TimeMap.loaders.metaweb#preload
<span class='line'> 89</span>      * @function
<span class='line'> 90</span>      * @parameter {Object} data     Data to preload
<span class='line'> 91</span>      * @return {Array} data         Array of item data
<span class='line'> 92</span>      */</span><span class="WHIT">
<span class='line'> 93</span> </span><span class="WHIT">    </span><span class="NAME">loader.preload</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 94</span> </span><span class="WHIT">        </span><span class="COMM">// Open outer envelope</span><span class="WHIT">
<span class='line'> 95</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">innerEnvelope</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">data.qname</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 96</span> </span><span class="WHIT">        </span><span class="COMM">// Make sure the query was successful</span><span class="WHIT">
<span class='line'> 97</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">innerEnvelope.code.indexOf</span><span class="PUNC">(</span><span class="STRN">"/api/status/ok"</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 98</span> </span><span class="WHIT">            </span><span class="COMM">// uncomment for debugging</span><span class="WHIT">
<span class='line'> 99</span> </span><span class="WHIT">            </span><span class="COMM">/*
<span class='line'>100</span>             // If error, get error message and throw
<span class='line'>101</span>             var error = innerEnvelope.messages[0];
<span class='line'>102</span>             throw error.code + ": " + error.message;
<span class='line'>103</span>             */</span><span class="WHIT">
<span class='line'>104</span> </span><span class="WHIT">            </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>105</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>106</span> </span><span class="WHIT">        </span><span class="COMM">// Get result from inner envelope</span><span class="WHIT">
<span class='line'>107</span> </span><span class="WHIT">        </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">innerEnvelope.result</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>108</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>109</span> 
<span class='line'>110</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">loader</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>111</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>112</span> </span></pre></body></html>